import urlConfig from './config.js'
import {
	queryParams
} from "./index.js"
const headers = {}
const PORT1 = '/baseinfo'
const request = function(parmas) {
	headers['Content-Type'] = 'application/x-www-form-urlencoded';
	headers['Authorization'] = `Bearer ${uni.getStorageSync('token')}`
	return new Promise((resolve, reject) => {
		if ((parmas.parmas && parmas.method == 'get') || parmas.method == 'put') { //get
			parmas.url = parmas.url + queryParams(parmas.parmas, "?")
			// console.log(parmas.url)
		} else {
			// console.log(parmas.parmas)
		}
		console.log(urlConfig + parmas.url)
		uni.request({
			url: urlConfig + parmas.url,
			method: parmas.method,
			data: parmas.data,
			header: headers,
			// method: parmas.method,
			dataType: 'json',
			success: (res) => {
				if (res.statusCode === 200) {
					if (res.data.code === -1) {
						uni.showModal({
							title: '提示',
							content: '当前已经在另一台设备登陆，是否重新登陆？',
							showCancel: false,
							success: (res) => {
								if (res.confirm) {
									// console.log('用户点击确定');
									uni.reLaunch({
										url: '/pages/index/index'
									})
								} else if (res.cancel) {
									// console.log('用户点击取消');
									return
								}
							}
						});
					} else if (res.data.code !== 1) {
						uni.showToast({
							title: res.data.msg,
							icon: "none",
							duration: 2000
						})
						resolve(res.data)
					}
					resolve(res.data);
				} else if (res.statusCode === 500) {
					uni.showToast({
						title: "系统错误",
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: parmas.message,
						icon: 'none'
					})
					reject(res)
				}
			},
			fail: function(res) {
				reject(res); // rejected
			}
		});

	});
}
export default request
